package org.codegallery.javagal.xml.axiom;

import org.codegallery.javagal.Constants;
import org.apache.axiom.om.*;
import org.apache.axiom.om.util.StAXUtils;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: gongwenwei
 * Date: 11-10-18
 * Time: 上午9:42
 * To change this template use File | Settings | File Templates.
 */
public class SampAxiom {
    public static void main(String[] args){
    	SampAxiom samp=new SampAxiom();
    	OMElement root=samp.build();
    	samp.search(root);
    }

    public OMElement build(){
    	OMElement root=null;
    	try{
    		//create a factory
            OMFactory factory = OMAbstractFactory.getOMFactory();
            //use the factory to create two namespace objects
            OMNamespace ns1 = factory.createOMNamespace("http://www.example.com/x","x");
            //use the factory to create three elements
            root = factory.createOMElement("root",ns1);
            OMNamespace ns2 = root.declareNamespace("http://www.example.com/y","y");
            OMElement elt11 = factory.createOMElement("foo1",ns2);
            OMElement bala = factory.createOMElement("bala",ns2);
            bala.setText("balalalal");
            elt11.addChild(bala);
            OMElement elt12 = factory.createOMElement("foo2",ns2);
            root.addChild(elt11);
            root.addChild(elt12);
            XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
            //dump the output to console with caching
            root.serialize(writer); 
            System.out.println();
            
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
    	return root;
    }
    
    public void traverse(OMElement root){
    	Iterator children = root.getChildren();
    	while(children.hasNext()){
    	        OMNode node = (OMNode)children.next();
    	        System.out.println(node.toString());
    	}
    }
    
    public void search(OMElement root){
    	OMElement foo1Element=root.getFirstChildWithName(new QName("http://www.example.com/y","foo1","y"));
    	System.out.println(foo1Element);
        
    }

    public void buildFromFile() throws Exception{
        //create the input stream
        InputStream in = new FileInputStream(Constants.TEST_RES_DIR+"\\xsltsample\\sample1.xml");

        //create the builder
        OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(in);

        //get the root element
        OMElement documentElement = builder.getDocumentElement();
    }

    public void usingStAXUtils() throws Exception{
        InputStream in = new FileInputStream(Constants.TEST_RES_DIR+"\\xsltsample\\sample1.xml");

        XMLStreamReader reader = StAXUtils.createXMLStreamReader(in);
    }
}
